// @import
import HeroCtrl, { g_oHeroCtrlMap } from "../ctrl/HeroCtrl";
import HeroCtrlFactory from "../ctrl/HeroCtrlFactory";
import MsgBus from "../comm/network/MsgBus";
import SceneData from "../data/SceneData";
import Time from "../comm/base/Time";
import UserData from "../data/UserData";
import { __UI_onLoad } from "./GameScene.UI";

// @import
import mod_async = require("async");
import mod_gameMsgProtocol = require("../msg/mod_gameMsgProtocol");

// @const
const { ccclass, } = cc._decorator;

/**
 * 游戏场景
 */
@ccclass
export default class GameScene extends cc.Component {
    /**
     * 主摄像机节点
     */
    _oMainCameraNode: cc.Node;

    /**
     * 我的英雄节点
     */
    _oMyHeroNode: cc.Node;

    /**
     * onLoad
     */
    onLoad(): void {
        SceneData._strCurrSceneName = "GameScene";

        this._oMainCameraNode = cc.find("/Canvas/_MainCamera");
        __UI_onLoad(this);
    }

    /**
     * start
     */
    start(): void {
        cc.director.getCollisionManager().enabled = true;
        //cc.director.getCollisionManager().enabledDebugDraw = true;

        cc.tween(cc.find("/Canvas/UI/Layout_BottomBar/Button_Help/Sprite_BG"))
            .sequence(
                cc.tween().to(0.1, { angle: +10, }),
                cc.tween().to(0.2, { angle: -20, }),
                cc.tween().delay(1),
            )
            .repeatForever()
            .start();

        mod_async.series([
            (funDoNext) => {
                HeroCtrlFactory.createByAvatar(UserData.getMyData()._strHeroAvatar, (oNewHeroNode) => {
                    if (null == oNewHeroNode) {
                        return;
                    }

                    this._oMyHeroNode = oNewHeroNode;
                    let oHeroCtrl: HeroCtrl = oNewHeroNode.getComponent(HeroCtrl);
                    oHeroCtrl._nMaxHP = 1;
                    oHeroCtrl._nCurrHP = 1;

                    // 设置英雄控件到字典
                    g_oHeroCtrlMap.set(
                        UserData.getMyData()._nUserId,
                        oHeroCtrl
                    );

                    cc.find("/Canvas/Holder_HeroGroup").addChild(oNewHeroNode);
                    funDoNext();
                });
            },

            () => {
                MsgBus.getInstance().sendMsg(
                    mod_gameMsgProtocol.msg.MsgCode.USER_ENTRY_CMD,
                    mod_gameMsgProtocol.msg.UserEntryCmd.create({
                    })
                );

                MsgBus.getInstance().sendMsg(
                    mod_gameMsgProtocol.msg.MsgCode.WHO_ELSE_IS_HERE_CMD,
                    mod_gameMsgProtocol.msg.WhoElseIsHereCmd.create({
                    })
                );
            }
        ]);
    }

    /**
     * lateUpdate
     */
    lateUpdate(): void {
        if (null == this._oMainCameraNode ||
            null == this._oMyHeroNode) {
            return;
        }

        let nTargetPosX = this._oMyHeroNode.x;
        let nTargetPosY = this._oMyHeroNode.y + 64;

        this._oMainCameraNode.x = cc.misc.lerp(this._oMainCameraNode.x, nTargetPosX, 5 * Time.getDeltaTime());
        this._oMainCameraNode.y = cc.misc.lerp(this._oMainCameraNode.y, nTargetPosY, 5 * Time.getDeltaTime());
    }
}
